﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BetRollParlay.aspx.cs"
    Inherits="LotteryVote.BetRollParlay" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="JS/jquery.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server" onsubmit="setJson()">
    <table>
        <tr>
            <td>
                Number1
            </td>
            <td>
                Number2
            </td>
            <td>
                Number3
            </td>
            <td>
                Amount
            </td>
            <td>
                Company
            </td>
            <td>
                Total Amt
            </td>
        </tr>
        <%for (int i = 1; i <= 10; i++)
          { %>
        <tr>
            <%for (int j = 1; j <= 3; j++)
              { %>
            <td>
                <input type="text" style="width: 60px" numno='<%=j%>' class='num<%=i %>' maxlength="2" />
            </td>
            <%} %>
            <td>
                <input type="text" style="width: 60px" rowid='<%=i %>' class="amount" />
            </td>
            <td>
                <%foreach (var company in TodayCompany)
                  { %>
                <input name='company<%=company.CompanyId+"_"+i %>' id='company<%=company.CompanyId+"_"+i %>'
                    type="checkbox" class='company<%=i %>' company='<%=company.CompanyId %>' />
                <label for='company<%=company.CompanyId+"_"+i %>'>
                    <%=company.Abbreviation %></label>
                <%} %>
            </td>
        </tr>
        <%} %>
    </table>
    <asp:HiddenField ID="hd_json" runat="server" />
    <a href="javascript:printJson()">Get Json</a><asp:Button ID="btn_addBet" runat="server"
        Text="AddBet" onclick="btn_addBet_Click" />
    <div id="json">
    </div>
    <script type="text/javascript">

        var printJson = function () {
            $("#json").text(transToJson());
        }

        var setJson = function () {
            $("#<%=hd_json.ClientID %>").val(transToJson());
        }

        var transToJson = function () {
            PL2 = 25;
            PL3 = 26;   //TODO：需程序设定包组过关的游戏玩法Id 
            var json = parseToRollParlay(getNotNullAmounts());
            return json;
        }

        //获取内容不为空的金额txtBox
        var getNotNullAmounts = function () {
            var amounts = $(".amount");
            var noNullAmounts = new Array();
            amounts.each(function (i, obj) {
                obj = $(obj);
                if (!isNullOrEmpty(obj.val()))
                    noNullAmounts.push(obj);
            });
            return noNullAmounts;
        }

        var parseToRollParlay = function (notNullAmount) {
            notNullAmount = $(notNullAmount);
            var betArr = new Array();
            notNullAmount.each(function (i, obj) {
                obj = $(obj);
                var rowId = obj.attr("rowid");
                var nums = $(".num" + rowId);
                if (nums.length != 3) return true;     //号码个数必须是3个，否则跳过

                var notNullNumArr = new Array();
                nums.each(function (x, num) {
                    num = $(num);
                    var numVal = num.val();
                    if (!isNullOrEmpty(numVal) && !isNaN(numVal))    //不能为空，且为数字
                        notNullNumArr.push(numVal);
                });

                //包组过关最少需有两个数字
                if (notNullNumArr.length < 2)
                    return true;

                var company = $(".company" + rowId);
                var checkCompany = new Array();     //获取选中的公司
                company.each(function (x, chk) {
                    chk = $(chk);
                    if (!isCheck(chk)) return true;
                    checkCompany.push(chk.attr("company"));
                });
                var numStr = notNullNumArr.join("#");
                var companyStr = "[" + checkCompany.join() + "]";
                var wargerStr = "[" + buildWarger(notNullNumArr.length, rowId) + "]";
                var subJson = "{\"CompanyList\":" + companyStr + ",\"Num\":\"" + numStr + "\",\"WargerList\":" + wargerStr + "}";
                betArr.push(subJson);
            });
            return "[" + betArr.join() + "]";
        }
        var buildWarger = function (numCount, rowId) {
            var gamePlayType = numCount == 2 ? PL2 : PL3;
            var amount;
            $(".amount").each(function (i, obj) {
                obj = $(obj);
                if (obj.attr("rowid") == rowId) {
                    amount = obj.val();
                    return false;
                }
            });
            var json = "{\"GamePlayTypeId\":" + gamePlayType + ",\"IsFullPermutation\":false,\"Wager\":" + amount + "}";
            return json;
        }
        var isCheck = function (chkItem) {
            var checkedVal = $(chkItem).attr("checked");
            return !(checkedVal == undefined || eval(checkedVal) == false)
        }
        var isNullOrEmpty = function (str) {
            return str == null || str == "" || str == undefined;
        }
    </script>
    </form>
</body>
</html>
